package be.dch.gui;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.Window;
import java.awt.image.ImageObserver;
import java.net.URL;

import org.apache.log4j.Logger;

import be.dch.util.StringUtils;

/**
 * Present a simple graphic to the user upon launch of the application, to
 * provide a faster initial response than is possible with the main window.
 * 
 * @author EH053
 */
final class SplashScreen extends Frame {

    /** * */
    private static final long serialVersionUID = 1L;
    private static final Logger LOG = Logger.getLogger(SplashScreen.class);

    private final transient String fImageId;
    private transient MediaTracker fMediaTracker;
    private transient Image fImage;
    private static final ImageObserver NO_OBSERVER = null;
    private static final int IMAGE_ID = 0;

    /**
     * Construct using an image for the splash screen.
     * 
     * @param aImageId
     */
    SplashScreen(final String aImageId) {
	super();
	if (StringUtils.isEmpty(aImageId)) {
	    throw new IllegalArgumentException("Image Id does not have content.");
	}
	this.fImageId = aImageId;
    }

    /**
     * Show the splash screen to the end user.
     * 
     */
    public void splash() {
	this.initImageAndTracker();
	this.setSize(this.fImage.getWidth(SplashScreen.NO_OBSERVER), this.fImage.getHeight(SplashScreen.NO_OBSERVER));
	this.center();

	this.fMediaTracker.addImage(this.fImage, SplashScreen.IMAGE_ID);
	try {
	    this.fMediaTracker.waitForID(SplashScreen.IMAGE_ID);
	} catch (final InterruptedException ex) {
	    SplashScreen.LOG.error("Cannot track image load.");
	}

	new SplashWindow(this, this.fImage);
    }

    /**
     * Initialise the image and the tracker.
     */
    private void initImageAndTracker() {
	this.fMediaTracker = new MediaTracker(this);
	final URL imageURL = SplashScreen.class.getResource(this.fImageId);
	this.fImage = Toolkit.getDefaultToolkit().getImage(imageURL);
    }

    /**
     * Centers the frame on the screen.
     */
    private void center() {
	final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
	final Rectangle frame = this.getBounds();
	this.setLocation((screen.width - frame.width) / 2, (screen.height - frame.height) / 2);
    }

    private class SplashWindow extends Window {
	private static final long serialVersionUID = 1L;
	private transient final Image fImage;

	SplashWindow(final Frame aParent, final Image aImage) {
	    super(aParent);
	    this.fImage = aImage;
	    this.setSize(this.fImage.getWidth(SplashScreen.NO_OBSERVER), this.fImage.getHeight(SplashScreen.NO_OBSERVER));
	    final Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
	    final Rectangle window = this.getBounds();
	    this.setLocation((screen.width - window.width) / 2, (screen.height - window.height) / 2);
	    this.setVisible(true);
	}

	@Override
	public void paint(final Graphics graphics) {
	    if (this.fImage != null) {
		graphics.drawImage(this.fImage, 0, 0, this);
	    }
	}

    }

}
